/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.fee.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.fee.entity.Cope;
import com.jeeplus.modules.fee.mapper.CopeMapper;
import com.jeeplus.modules.fee.entity.CopeDetil;
import com.jeeplus.modules.fee.mapper.CopeDetilMapper;

/**
 * 应付Service
 * @author 李超辉
 * @version 2018-02-01
 */
@Service
@Transactional(readOnly = true)
public class CopeService extends CrudService<CopeMapper, Cope> {

	@Autowired
	private CopeDetilMapper copeDetilMapper;
	
	public Cope get(String id) {
		Cope cope = super.get(id);
		cope.setCopeDetilList(copeDetilMapper.findList(new CopeDetil(cope)));
		return cope;
	}
	
	public List<Cope> findList(Cope cope) {
		return super.findList(cope);
	}
	
	public Page<Cope> findPage(Page<Cope> page, Cope cope) {
		return super.findPage(page, cope);
	}
	
	@Transactional(readOnly = false)
	public void save(Cope cope) {
		super.save(cope);
		for (CopeDetil copeDetil : cope.getCopeDetilList()){
			if (copeDetil.getId() == null){
				continue;
			}
			if (CopeDetil.DEL_FLAG_NORMAL.equals(copeDetil.getDelFlag())){
				if (StringUtils.isBlank(copeDetil.getId())){
					copeDetil.setCope(cope);
					copeDetil.preInsert();
					copeDetilMapper.insert(copeDetil);
				}else{
					copeDetil.preUpdate();
					copeDetilMapper.update(copeDetil);
				}
			}else{
				copeDetilMapper.delete(copeDetil);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(Cope cope) {
		super.delete(cope);
		copeDetilMapper.delete(new CopeDetil(cope.getId()));
	}
	
}